package cn.bupt.adt.controller;

import cn.bupt.adt.constant.Constants;
import cn.bupt.adt.entity.autogenerate.InspectOrder;
import cn.bupt.adt.entity.autogenerate.InspectOrderView;
import cn.bupt.adt.model.InspectAllOrderModel;
import cn.bupt.adt.model.InspectOrderModel;
import cn.bupt.adt.model.inspectSearchModel;
import cn.bupt.adt.service.IinspectOrderService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/InspectOrderController")
public class InspectOrderController {

    @Autowired
    IinspectOrderService inspectOrderService;

    // 查看订单详情
    @RequestMapping("/detail/{id}")
    public ModelAndView detail(@PathVariable("id") int id) {
        ModelAndView mv = new ModelAndView(Constants.INSPECTORDER_DETAIL);
        InspectOrder order = inspectOrderService.getOrderById(id);
        mv.addObject("order", order);
        return mv;
    }

    @RequestMapping("/index/{pageNum}")
    public ModelAndView InspectIndex(@PathVariable("pageNum") int pageNum, HttpServletRequest request,
                                     inspectSearchModel inspectSearchModel) {
        ModelAndView mv = new ModelAndView(Constants.INSPECTORDER_INDEX);
        Page<?> page = PageHelper.startPage(pageNum, Constants.PAGESIZE_LARGE, "id desc");
        List<InspectAllOrderModel> inspects = null;
        if (inspectSearchModel.getSearchway() == null) {
            inspects = inspectOrderService.getAllInspectItemsOrders();
        } else {
            // if(inspectSearchModel.getSearchway().equals(Constants.WITH_NAME))
            // {
            // inspects =
            // inspectOrderService.getOrderByName(inspectSearchModel.getContent());
            // }
            // else{
            // inspects =
            // inspectOrderService.getOrderByNumber(inspectSearchModel.getContent());
            // }
            // mv.addObject("searchMethod", inspectSearchModel.getSearchway());
            // mv.addObject("content", inspectSearchModel.getContent());
        }
        mv.addObject("currentPage", page.getPageNum());
        mv.addObject("allPages", page.getPages());
        mv.addObject("inspects", inspects);
        return mv;
    }

    // 已接单检查项目订单
    @RequestMapping("/indexTaked/{pageNum}")
    public ModelAndView InspectTakedIndex(@PathVariable("pageNum") int pageNum, HttpServletRequest request,
                                          inspectSearchModel inspectSearchModel) {
        ModelAndView mv = new ModelAndView(Constants.INSPECTORDER_INDEXTAKED);
        Page<?> page = PageHelper.startPage(pageNum, Constants.PAGESIZE_LARGE, "id desc");
        List<InspectOrderModel> inspects = null;
        if (inspectSearchModel.getSearchway() == null) {
            inspects = inspectOrderService.getAllInspectOrders();
        } else {
            if (inspectSearchModel.getSearchway().equals(Constants.WITH_NAME)) {
                inspects = inspectOrderService.getOrderByName(inspectSearchModel.getContent());
            } else {
                inspects = inspectOrderService.getOrderByNumber(inspectSearchModel.getContent());
            }
            mv.addObject("searchMethod", inspectSearchModel.getSearchway());
            mv.addObject("content", inspectSearchModel.getContent());
        }
        mv.addObject("currentPage", page.getPageNum());
        mv.addObject("allPages", page.getPages());
        mv.addObject("inspects", inspects);
        return mv;
    }

    // 查看订单详情
    @RequestMapping("/detailTaked/{id}")
    public ModelAndView tackedDetail(@PathVariable("id") int id) {
        ModelAndView mv = new ModelAndView(Constants.INSPECTORDER_DETAILTAKED);
        InspectOrderView order = inspectOrderService.getOrderViewById(id);
        mv.addObject("order", order);
        return mv;
    }
}
